

# from fastapi import Depends

# def dep_a(): 
#     return {"a": 1}

# def dep_b(a: dict = Depends(dep_a)): 
#     return {**a, "b": 2}  # 依赖 dep_a


# print(dep_b(dep_a()))

# @app.get("/test")
# async def test(b: dict = Depends(dep_b)):  # 依赖 dep_b
#     return b  # 输出: {"a": 1, "b": 2}













# import os
# import uuid
# from fastapi import FastAPI, UploadFile, File, HTTPException
# from fastapi.responses import JSONResponse
# from starlette.status import HTTP_413_REQUEST_ENTITY_TOO_LARGE

# app = FastAPI()

# # 配置常量
# UPLOAD_DIR = "uploads"
# MAX_FILE_SIZE = 1024 * 1024 * 100  # 100MB
# ALLOWED_TYPES = {"image/jpeg", "image/png", "application/pdf"}

# # 确保上传目录存在
# os.makedirs(UPLOAD_DIR, exist_ok=True)

# @app.post("/upload")
# async def upload_file(file: UploadFile = File(...)):
#     """
#     文件上传接口，支持健壮性检查和流式处理
#     """
#     # 1. 验证文件是否为空
#     if not file:
#         raise HTTPException(400, "未检测到文件")

#     # 2. 验证文件类型
#     if file.content_type not in ALLOWED_TYPES:
#         raise HTTPException(400, f"不支持的文件类型: {file.content_type}")

#     # 3. 验证文件大小
#     try:
#         if file.size > MAX_FILE_SIZE:
#             raise HTTPException(
#                 HTTP_413_REQUEST_ENTITY_TOO_LARGE,
#                 f"文件大小超过限制 {MAX_FILE_SIZE//(1024 * 1024)}MB"
#             )
#     except AttributeError:
#         # 部分客户端可能不提供size属性
#         pass

#     # 4. 生成唯一文件名
#     file_ext = os.path.splitext(file.filename)[1]
#     unique_id = uuid.uuid4().hex
#     new_filename = f"{unique_id}{file_ext}"
#     save_path = os.path.join(UPLOAD_DIR, new_filename)

#     try:
#         # 5. 流式写入（内存高效）
#         with open(save_path, "wb") as f:
#             while chunk := await file.read(1024 * 1024):  # 每次读取1MB
#                 f.write(chunk)
                
#     except IOError as e:
#         raise HTTPException(500, f"文件保存失败: {str(e)}")
#     finally:
#         await file.close()

#     return JSONResponse(
#         status_code=201,
#         content={
#             "filename": new_filename,
#             "url": f"/download/{new_filename}",
#             "original_name": file.filename,
#             "size": os.path.getsize(save_path)
#         }
#     )

# @app.get("/download/{filename}")
# async def download_file(filename: str):
#     """文件下载接口"""
#     file_path = os.path.join(UPLOAD_DIR, filename)
#     if not os.path.exists(file_path):
#         raise HTTPException(404, "文件不存在")
#     return FileResponse(file_path, filename=filename)

# if __name__ == "__main__":
#     import uvicorn
#     uvicorn.run("demo2:app", host="0.0.0.0", port=8000, reload=True)




# import uvicorn
# from fastapi import Depends, FastAPI

# app = FastAPI()

# def test_depend(item_num: int = 1):
#     print(f"input: {item_num}")
#     item_num += 1
#     print(f"return: {item_num}")
#     return item_num

# @app.get("/items/{item_num}")
# async def desplay_items(item_num : int = Depends(test_depend)):
#     return {"back": item_num}


# @app.get("/items2/{item_id}")
# async def read_item(item_id: int = Depends(test_depend)):
#     print(f"----------{item_id}")
#     return {"item_id": item_id, "q": f"接口id：{item_id}"}


# @app.get("/items3/{item_num}")
# async def read_item(item_num: int = Depends(test_depend)):
#     print(f"----------{item_num}")
#     return {"item_id": item_num, "q": f"接口id：{item_num}"}


# @app.get("/")
# def index():
#     return {"contex":"helloworld"}


# if __name__ == "__main__":
#     uvicorn.run("demo2:app", host="0.0.0.0", port=8000, reload=True)






import uvicorn
from fastapi import Depends, FastAPI

app = FastAPI()

def test_depend(item_num: int = 1): # ----------item_num 是从 url中传的----------
    item_num += 1
    return item_num

def test_depend2(item_id: int = 1):
    item_id += 1
    return item_id

@app.get("/items2/{item_id}")
async def read_item(item_id: int = Depends(test_depend2)):   # -----item_id-的值是从依赖函数返回的--------------
    print(f"----------{item_id}")
    return {"item_id": item_id, "q": f"接口id：{item_id}"}

@app.get("/items5/{item_id}")
async def read_item(item_id_new: int = Depends(test_depend2)):
    print(f"----------{item_id_new}")
    return {"item_id_new": item_id_new, "q": f"接口id：{item_id_new}"}

@app.get("/items3/{item_num}")
async def read_item(item_num: int = Depends(test_depend)):
    print(f"----------{item_num}")
    return {"item_id": item_num, "q": f"接口id：{item_num}"}


@app.get("/items1/{item_num}")
async def read_item(item_num: int):
    print(f"----------{item_num}")
    return {"item_id": item_num, "q": f"接口id：{item_num}"}

if __name__ == "__main__":
    uvicorn.run("demo2:app", host="0.0.0.0", port=8000, reload=True)